﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TechnicBlog.Models;

namespace TechnicBlog.Controllers.StudentManager
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/

        public ActionResult Index(int id)
        {
            DBContext db = new DBContext();
            ClassesEntity model = db.Classes.Find(id);
            return View(model);
        }

        public string List()
        {
            DBContext db = new DBContext();
            int pageindex = Convert.ToInt32(Request.Form["page"]);
            int pagesize = Convert.ToInt32(Request.Form["rows"]);
            string S_Name = "";
            int classesId = 0;
            if (Request.QueryString["S_Name"] != null)
                S_Name = Request.QueryString["S_Name"].ToString();
            if (Request.QueryString["ClassesId"] != null)
                classesId = Convert.ToInt32(Request.QueryString["ClassesId"].ToString());
            IList<StudentEntity> list = db.Students.ToList();
            var result = from u in list
                         orderby u.S_Id descending
                         where u.S_Name.Contains(S_Name)
                         where u.S_ClassesId.Equals(classesId)
                         select u;
            List<StudentEntity> l = (result).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList();
            int total = result.Count();
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            //这里使用自定义日期格式，如果不使用的话，默认是ISO8601格式           
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd";
            //jsonObject是准备转换的对象
            string s = "{\"total\":" + total + ",\"rows\":" + Newtonsoft.Json.JsonConvert.SerializeObject(l, Newtonsoft.Json.Formatting.Indented, timeConverter) + "}";
            db.Dispose();
            return s;
        }

        public ActionResult Create(int id,StudentEntity model)
        {
            DBContext db = new DBContext();
            model.Classes = db.Classes.Find(id);
            model.S_ClassesId = id;
            return View(model);
        }

        public ActionResult Add(StudentEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                db.Students.Add(model);
                ClassesEntity classModel = db.Classes.Find(model.S_ClassesId);
                classModel.StudentNum++;
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>恭喜您！新增成功！','info');TreeLink('Student/Index/"+model.S_ClassesId+"', '学生基本信息列表');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>新增失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Edit(int id)
        {
            DBContext db = new DBContext();
            StudentEntity model = db.Students.Find(id);
            //SelectList ClassesList = new SelectList(db.Classes.ToList(), "Id", "ClassName",model.S_ClassesId);
            //ViewBag.ClassesList = ClassesList;
            db.Dispose();
            if (model == null)
            {
                RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(StudentEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                StudentEntity student = db.Students.Find(model.S_Id);
                UpdateModel(student);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>恭喜您！编辑成功！','info');TreeLink('Student/Index/"+model.S_ClassesId+"', '学生基本信息列表');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>编辑失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Delete(int id)
        {
            DBContext db = new DBContext();
            int sId = Convert.ToInt32(Request.QueryString["S_Id"].ToString());
            StudentEntity model = db.Students.Find(sId);
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>要删除的数据不存在，或已被删除！','info'); $('#rptList').datagrid('reload');");
            }
            db.Students.Remove(model);
            ClassesEntity classModel = db.Classes.Find(id);
            classModel.StudentNum--;
            db.SaveChanges();
            db.Dispose();
            return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>删除成功！','info');$('#rptList').datagrid('reload');");
        }
    }
}
